Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

215
Vistas
Why I am getting age[i] as undefined?

Here I am going to describe my problem that why I am getting age[i]=a as undefined when I am going to console it

enter image description here

const birthYear = [1996, 1997, 1998, 1999]

function calc(Year) {
  age = 2022 - Year;
  return age

}
var age = []
age[0] = 123;
console.log(age[0])

for (i = 0; i <= birthYear.length - 1; i++) {
  var a = calc(birthYear[i])

  /* if you want to return the value of any function you have to  address 
     it the help of any agruement otherwise it will show you a function body
   */
  age[i] = a
  // console.log
  console.log(age[i])
}

// console.log(age[])

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The problem is that you return "age" with calc and also called your array "age". Hence the array gets replaced everytime and you're getting "undefined".

Try this :

const birthYear=[1996,1997,1998,1999]

function calc(Year) {
    age=2022-Year;
    return age
}

var ageArr = [];

for(var i = 0 ; i < birthYear.length ; i++){
    ageArr[i] = calc(birthYear[i])
    console.log(ageArr[i]);
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

why I am getting age[i] as undefined?

you have used var keyword to declare age array,now the age become global variable,so when you make a call to calC function it converts array to number age=2022-Year; after executing this statement . thats the reason age[i] is undefined . now whats the correct way to do this,just declare ageCount inside calc function and return it,like this

 const birthYear=[1996,1997,1998,1999]
 function calc(Year) 
 {
   //instead of using 2022, you fetch current year from date object
   let ageCount=2022-Year;
   return ageCateCount
}
var age=[]
age[0]=123;
console.log(age[0])

for(i=0;i<=birthYear.length-1;i++){
   age[i]=calc(birthYear[i]);
// console.log

         console.log(age[i])
}

const birthYear = [1996, 1997, 1998, 1999]

function calc(Year) {
  let ageCount = new Date().getFullYear() - Year;
  return ageCount

}
var age = []
age[0] = 123;
console.log(age[0])

for (i = 0; i <= birthYear.length - 1; i++) {
  var a = calc(birthYear[i])

  /* if you want to return the value of any function you have to  address 
     it the help of any agruement otherwise it will show you a function body
   */
  age[i] = a
  // console.log
  console.log(age[i])
}

hope this will help!

about 4 years ago · Juan Pablo Isaza Denunciar

0

If I understand the requirement correctly, You want to find the difference from year 2022 to the years in the birthYear array. If Yes, You can simply achieve this with a single line of code instead of making it complex.

Demo :

const birthYear = [1996, 1997, 1998, 1999];

const age = birthYear.map(year => 2022 - year);

console.log(age);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda